package com.jsmile.cloud.accountcenter.account.feign;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.accountcenter.account.service.UserAddressApplicationService;
import com.jsmile.cloud.accountcenter.api.req.address.ReqAddressDetial;
import com.jsmile.cloud.accountcenter.api.req.address.ReqAddressPageQuery;
import com.jsmile.cloud.accountcenter.api.req.address.ReqAddressSave;
import com.jsmile.cloud.accountcenter.api.req.address.ReqAddressUpdate;
import com.jsmile.cloud.accountcenter.api.res.ResUserAddress;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.api.to.BaseTo;
import com.jsmile.mall.api.to.JsmilePage;
import com.jsmile.mall.log.annotation.JsmileLog;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@AllArgsConstructor
public class AddressServiceApiImpl extends AuthcController {

    private final UserAddressApplicationService userAddressApplicationService;

    @RequestMapping(value = "/feign/address/getUserAddress", method = RequestMethod.POST)
    public JSmileResult<ResUserAddress> getUserAddress(@RequestBody @Valid ReqAddressDetial reqAddressDelete, @CurrentContext JsmileContext jsmileContext) {
        ResUserAddress userAddress = userAddressApplicationService.getUserAddress(reqAddressDelete.getAddressId());
        return JSmileResult.success(userAddress);
    }

    @RequestMapping(value = "/feign/address/addUserAddress", method = RequestMethod.POST)
    @JsmileLog(title = "账户中心->新增地址", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.ACCOUNT_CENTER)
    public JSmileResult<Boolean> addUserAddress(@RequestBody @Valid ReqAddressSave reqAddressSave, @CurrentContext JsmileContext jsmileContext) {
        reqAddressSave.setCurUserId(jsmileContext.getUserId());
        reqAddressSave.setCurTenantId(jsmileContext.getTenantId());
        return userAddressApplicationService.addUserAddress(reqAddressSave) ? JSmileResult.success("地址保存成功") : JSmileResult.error("地址保存失败");
    }

    @RequestMapping(value = "/feign/address/changeUserAddress", method = RequestMethod.POST)
    @JsmileLog(title = "账户中心->更新地址", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.ACCOUNT_CENTER)
    public JSmileResult<Boolean> changeUserAddress(@RequestBody @Valid ReqAddressUpdate reqAddressUpdate, @CurrentContext JsmileContext jsmileContext) {
        reqAddressUpdate.setCurUserId(jsmileContext.getUserId());
        reqAddressUpdate.setCurTenantId(jsmileContext.getTenantId());
        return userAddressApplicationService.changeUserAddress(reqAddressUpdate) ? JSmileResult.success("地址更新成功") : JSmileResult.error("地址更新失败");
    }

    @RequestMapping(value = "/feign/address/getDefaultUserAddress", method = RequestMethod.POST)
    @JsmileLog(title = "账户中心->获取默认地址", type = LogTypeEnum.QUERY, systemId = JSmileConstant.SystemId.ACCOUNT_CENTER)
    public JSmileResult<ResUserAddress> getDefaultUserAddress(@RequestBody @Valid BaseTo baseTo, @CurrentContext JsmileContext jsmileContext) {
        return JSmileResult.success(userAddressApplicationService.getDefaultUserAddress(jsmileContext.getUserId()));
    }

    @RequestMapping(value = "/feign/address/deleteUserAddress", method = RequestMethod.POST)
    @JsmileLog(title = "账户中心->删除用户地址", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.ACCOUNT_CENTER)
    public JSmileResult<Boolean> deleteUserAddress(@RequestBody @Valid ReqAddressDetial reqAddressDelete, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.ACCOUNT_CENTER);
        reqAddressDelete.setCurUserId(jsmileContext.getUserId());
        reqAddressDelete.setCurTenantId(jsmileContext.getTenantId());
        return userAddressApplicationService.deleteUserAddress(reqAddressDelete) ? JSmileResult.success("地址删除成功") : JSmileResult.error("地址删除失败");
    }

    @RequestMapping(value = "/feign/address/getUserAddressPageList", method = RequestMethod.POST)
    @JsmileLog(title = "账户中心->分页获取地址", type = LogTypeEnum.QUERY, systemId = JSmileConstant.SystemId.ACCOUNT_CENTER)
    public JSmileResult<JsmilePage<ResUserAddress>> getUserAddressPageList(@RequestBody @Valid ReqAddressPageQuery reqAddressPageQuery,
        @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.ACCOUNT_CENTER);
        reqAddressPageQuery.setCurUserId(jsmileContext.getUserId());
        reqAddressPageQuery.setCurTenantId(jsmileContext.getTenantId());
        IPage<ResUserAddress> pages = userAddressApplicationService.getUserAddressPageList(reqAddressPageQuery);
        return JSmileResult.success(new JsmilePage(reqAddressPageQuery.getPageIndex(), reqAddressPageQuery.getPageSize(), pages.getTotal(), pages.getRecords()));
    }
}
